package com.sidneysimmons.financially.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.sidneysimmons.financially.Account;
import com.sidneysimmons.financially.R;
import com.sidneysimmons.financially.activities.AccountActivity;

public class EditAccountDialogFragment extends DialogFragment {
	
	/** Used by the FragmentManager to identify this fragment. */
	public static String TAG = "edit_account_dialog";
	
	private View editAccountView;
	
	public static EditAccountDialogFragment newInstance() {
		EditAccountDialogFragment fragment = new EditAccountDialogFragment();
		
		/* // We could dynamically put data into the dialog here
        Bundle bundle = new Bundle();
        bundle.putInt("title", title);
        fragment.setArguments(bundle); */
    	
        return fragment;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Use this to get dynamically put data, everything is static now
		// int title = getArguments().getInt("title");

		// Inflate the view to use
		Context context = getActivity();
		LayoutInflater inflater = LayoutInflater.from(context);
		editAccountView = inflater.inflate(R.layout.edit_account_dialog, null);
		
		// Populate the view with the existing category data
		populateExistingAccountView(editAccountView);
		
		return new AlertDialog.Builder(context)
		.setView(editAccountView)
        .setPositiveButton(R.string.edit_account_dialog_text_save,
            new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// populate Account and send back to UI
					AccountActivity activity = (AccountActivity) getActivity();
					activity.validateAndEditAccount(activity.getAccountInFocus(), populateUpdatedAccount(editAccountView));
				}
            }
        )
        .setNegativeButton(R.string.edit_account_dialog_text_delete,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	// Delete the account
                	AccountActivity activity = (AccountActivity) getActivity();
                	activity.deleteAccount(activity.getAccountInFocus());
                }
            }
        )
        .create();
	}
	
	/**
	 * Fill in the fields on the dialog with the existing account information.
	 * 
	 * @param editAccountView - the View to get the data from
	 */
	protected void populateExistingAccountView(View editAccountView) {
		Account originalAccount = ((AccountActivity)getActivity()).getAccountInFocus();
		
		// Get fields here
		EditText accountNameField = (EditText) editAccountView.findViewById(R.id.edit_account_dialog_edittext_account_name);

		// Set the data here
		accountNameField.setText(originalAccount.getTitle());
	}
	
	/**
     * Get the newly entered data from the fields on the dialog.
     * 
     * @param editAccountView - the View to get the data from
     * @return a populated Account object
     */
    protected Account populateUpdatedAccount(View editAccountView) {
    	Account account = new Account();
    	
    	// Get fields here
    	EditText accountNameField = (EditText) editAccountView.findViewById(R.id.edit_account_dialog_edittext_account_name);
    	
    	// Set data here
    	account.setTitle(accountNameField.getText().toString().trim());
    	
    	return account;
    }
}